<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: setting.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
class setting extends admin{
	function __construct() {
		parent::__construct();
		$this->main();
	}

	function main(){
		if(isset($_GET['action'])) {
			if(method_exists($this, 'tr_'.$_GET['action'])){
				$this->form = ADMNAME.'?mod=setting&action=ajaxpost';
				include load_tpl('form');
			}elseif($_GET['action']=='ajaxpost' && !empty($_POST)){
				$this->ajaxPost();
			}
		}
	}

	function display(){
		echo '<div class="tb_t">'.$this->title.'</div>';
		echo '<table class="tb">';
		$this->{'tr_'.$_GET['action']}();
		echo '</table>';
		echo '<div class="tb_b"><input type="submit" value="&#20445;&#23384;" class="but1" /></div>';
	}

	function tr_basic(){
		load_tr('站点标题', $this->G['webname'], 'text', 'webname');
		load_tr('站点域名', $this->G['webdomain'], 'text', 'webdomain');
		load_tr('所在目录', $this->G['webdir'], 'text', 'webdir');
		load_tr('邮箱', $this->G['webmail'], 'text', 'webmail');
		load_tr('统计代码', $this->G['tongji'], 'textarea', 'tongji');
		load_tr('备案号', $this->G['beian'], 'text', 'beian');
	}

	function tr_cache(){
		load_tr('开启缓存', $this->G['iscache'], 'yesno', 'iscache');
	}

	function tr_ishtml(){
		load_tr('开启静态', $this->G['ishtml'], 'yesno', 'ishtml');

		load_tr('静态文件目录', $this->G['html_dir'], 'text', 'html_dir');
		load_tr('默认页名称', $this->G['html_index'], 'text', 'html_index');

		load_tr('每次生成数量', $this->G['html_maxnum'], 'number', 'html_maxnum');
		load_tr('每次等待时间', $this->G['html_time'], 'number', 'html_time');
	}

	function tr_isfhtml(){
		load_tr('开启伪静态', $this->G['isfhtml'], 'yesno', 'isfhtml');

		$format='<tr><th>%s</th><td>前缀<input name="%s" type="text" value="%s" class="inp ws"> &nbsp; 后缀<input name="%s_suffix" type="text" value="%s" class="inp ws"></td></tr>';
		printf($format, '分类页URL', 'url_cate', $this->G['url_cate'], 'url_cate', $this->G['url_cate_suffix']);
		printf($format, '内容页URL', 'url_show', $this->G['url_show'], 'url_show', $this->G['url_show_suffix']);
		printf($format, '评论页URL', 'url_comment', $this->G['url_comment'], 'url_comment', $this->G['url_comment_suffix']);
		printf($format, 'Tags页URL', 'url_tags', $this->G['url_tags'], 'url_tags', $this->G['url_tags_suffix']);
		
		//load_tr('开启高级分类页URL', $this->G['custom_cate'], 'yesno', 'custom_cate');
		//load_tr('开启高级内容页URL', $this->G['custom_show'], 'yesno', 'custom_show');
	}

	function tr_attachment(){
		$format='<tr><th>%s</th><td>允许类型<input name="%s" type="text" value="%s" class="inp wa">最大限制<input name="%s" type="number" value="%s" class="inp ws">KB</td></tr>';
		printf($format, '上传图片', 'up_img_ext', $this->G['up_img_ext'], 'up_img_max_size', $this->G['up_img_max_size']);
		printf($format, '上传附件', 'up_file_ext', $this->G['up_file_ext'], 'up_file_max_size', $this->G['up_file_max_size']);
		printf($format, '远程抓取', 'get_file_ext', $this->G['get_file_ext'], 'get_file_max_size', $this->G['get_file_max_size']);
		//load_tr('缩略图宽度', $this->G['attwidth'], 'number', 'attwidth');
		//load_tr('缩略图高度', $this->G['attheight'], 'number', 'attheight');
	}

	function tr_seo(){
		load_tr('副标题', $this->G['seotitle'], 'text', 'seotitle');
		load_tr('SEO关键字', $this->G['seokeywords'], 'text', 'seokeywords');
		load_tr('SEO描述', $this->G['seodescription'], 'textarea', 'seodescription');
	}

	function ajaxPost(){
		$ret = $this->set_setting($_POST);
		ret_json($ret, '修改'.($ret ? '成功' : '失败'));
	}

	function set_setting($arr) {
		$ret=true;
		foreach($arr as $kay=>$val) {
			$kay = trim($kay);
			if(!empty($kay)) {
				if($kay=='iscache') {
					if($val=='1') f_write(TWCMS_CACHE.'iscache.txt', '');
					else @unlink(TWCMS_CACHE.'iscache.txt');
				}

				$val = is_array($val) ? implode(',',$val) : $val;
				if($this->db->result_first('SELECT id FROM  `'.TW_DBPRE.'setting` WHERE kay='.S($kay).' LIMIT 1')) {
					$this->db->query('UPDATE `'.TW_DBPRE.'setting` SET value='.S($val).' WHERE kay='.S($kay).' LIMIT 1') || $ret=false;
				}else{
					$this->db->query('INSERT INTO `'.TW_DBPRE.'setting`(`kay`,`value`)VALUES('.S($kay).','.S($val).')') || $ret=false;
				}
			}
		}
		@unlink(TWCMS_CACHE.'setting.php');
		return $ret;
	}
}